﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 软构作业Winform
{
    public partial class Form2 : Form
    {
        private double op1 = 0, op2 = 0;

        private string ope = "";

        private bool is_first = true;

        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }

        private void Num_button_Click(object sender, EventArgs e)
        {
            Button b = (Button)sender;
            if (is_first)
            {
                op1 = op1 * 10 + b.TabIndex;
                textBox1.Text = $"{op1}";
            }
            else
            {
                op2 = op2 * 10 + b.TabIndex;
                textBox1.Text = $"{op1} {ope} {op2}";
            }
            
        }

        private void Ope_button_Click(object sender, EventArgs e)
        {
            Button b = (Button)sender;
            is_first = false;
            ope = b.Text;
            textBox1.Text = $"{op1} {ope}";
        }

        private void Equal_button_Click(object sender, EventArgs e)
        {
            double result = 0;
            try
            {
                switch (ope)
                {
                    case "+": 
                        result = op1 + op2;
                        break;
                    case "-":
                        result = op1 - op2;
                        break;
                    case "×":
                        result = op1 * op2;
                        break;
                    case "÷":
                        result = op1 / op2;
                        break;
                    default:
                        result = op1;
                        break;
                }
                textBox1.Text = ope == "" ? $"{op1} = {result}" : $"{op1} {ope} {op2} = {result}";
            }
            catch(Exception ex)
            {
                textBox1.Text = $"Error:{ex.Message}";
            }
            op1 = op2 = 0;
            ope = "";
            is_first = true;
        }

        void Clear_button_Click(object sender,EventArgs e)
        {
            op1 = op2 = 0;
            ope = "";
            is_first = true;
            textBox1.Text = "";
        }
    }
}
